<script>
export default {
  name:"productUnit",
  data() {
    return {
      tableData:[],
      pageNum:1,
      pageSize:5,
      total:5,
      name:'',
      dialogVisible:false,
      loading: true,
      form:{
        id:'',
        name:'',
        basicUnit:'',
        otherUnit:'',
        otherUnitTwo:'',
        ratio:'',
        ratioTwo:'',
        status:''
      },
      rules: {
        basicUnit: [
          { required: true, message: '请输入基本单位(小单位)', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' },
        ],
        otherUnit: [
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' },
        ],
        otherUnitTwo: [
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' },
        ],
        ratio: [
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' },
        ],
        ratioTwo: [
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' },
        ],
      }
    };
  },
  methods:{
    loadPost(){
      this.loading = true;
      this.$axios.post('/zzh-product-unit/list',{
        page:this.pageNum,
        limit:this.pageSize,
        name:this.name
      }).then(res=>res.data).then(res=>{
        this.loading = false;
        if(res.resultCode==200){
          this.tableData=res.data
          this.total=res.total
        }else {
          alert('获取数据失败')
        }
      })
    },
    add(){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.resetForm()
      })
    },
    loadAdd(){
      //表单验证
      this.$refs.form.validate((valid) => {
        if(this.form.otherUnit && this.form.ratio){
          this.form.name=this.form.basicUnit+"/("+this.form.otherUnit+"="+this.form.ratio+this.form.basicUnit+")"
        }
        if(this.form.otherUnitTwo && this.form.ratioTwo){
          this.form.name+="/("+this.form.otherUnitTwo+"="+this.form.ratioTwo+this.form.basicUnit+")"
        }
        if (valid) {
          if(this.form.id){
            this.doUpdate();
            this.form.id='';
          }else {
            this.doAdd()
          }
        } else {
          return false;
        }
      });
    },
    doAdd(){
      this.$axios.post('/zzh-product-unit/add',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '新增数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '新增数据失败',
            type: 'error'
          });
        }
      })
    },
    mod(row){
      this.dialogVisible=true
      this.$nextTick(()=>{
        this.form.id=row.id
        this.form.name=row.name
        this.form.basicUnit=row.basicUnit
        this.form.otherUnit=row.otherUnit
        this.form.otherUnitTwo=row.otherUnitTwo
        this.form.ratio=row.ratio
        this.form.ratioTwo=row.ratioTwo
        this.form.status=row.status
      })
    },
    doUpdate(){
      this.$axios.put('/zzh-product-unit/update',this.form).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.dialogVisible=false
          this.$message({
            showClose: true,
            message: '更新数据成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '更新数据失败',
            type: 'error'
          });
        }
      })
    },
    del(id){
      this.$axios.delete('/zzh-product-unit/delete?id='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'error'
          });
        }
      })
    },
    updateStatus(id){
      this.$axios.put('/zzh-product-unit/updateStatus?id='+id).then(res=>res.data).then(res=>{
        if(res.resultCode==200){
          this.$message({
            showClose: true,
            message: '更新状态成功',
            type: 'success'
          });
          this.loadPost()
        }else {
          this.$message({
            showClose: true,
            message: '更新状态失败',
            type: 'error'
          });
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    resetParam(){
      this.name=''
      this.loadPost()
    },
    resetForm() {
      //重置窗体
      this.$refs.form.resetFields();
    }
  },
  beforeMount() {
    this.loadPost();
  }
}
</script>

<template>
  <div>
    <!--    按钮-->
    <div style="margin-bottom: 5px">
      <el-input v-model="name" placeholder="请输入名字" suffix-icon="el-icon-search" style="width: 200px"
                @keyup.enter.native="loadPost"></el-input>
      <el-button type="primary" style="margin-left: 5px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
    </div>
    <!--    表单-->
    <el-table :data="tableData" v-loading="loading"
              :header-cell-style="{background:'#f3f6fd',color:'#555'}">
      <el-table-column prop="id" label="id" width="140">
      </el-table-column>
      <el-table-column prop="name" label="计量单位" width="250">
      </el-table-column>
      <el-table-column prop="basicUnit" label="基本单位" width="140">
      </el-table-column>
      <el-table-column prop="otherUnit" label="副单位" width="120">
      </el-table-column>
      <el-table-column prop="otherUnitTwo" label="副单位2" width="120">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag
              :type="scope.row.status === 1 ? 'success' : 'danger'"
              disable-transitions>{{scope.row.status === 1 ? '正常' : scope.row.status === 2 ? '禁用' : '已删除'}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              style="margin-left: 5px;margin-right: 5px"
              @confirm="del(scope.row.id)">
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>
          <el-button size="small" type="success" @click="updateStatus(scope.row.id)">启用/禁用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!--    提示框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="40%"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="mini">
        <el-form-item label="基本单位" prop="basicUnit">
          <el-input v-model="form.basicUnit" placeholder="基本单位"></el-input>
        </el-form-item>
        <el-form-item label="副单位" prop="otherUnit">
          <el-input v-model="form.otherUnit" placeholder="请输入副单位(大单位)"></el-input>
        </el-form-item>
        <el-form-item label="比例"  prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入比例"></el-input>
        </el-form-item>
        <el-form-item label="副单位2" prop="otherUnitTwo">
          <el-input v-model="form.otherUnitTwo" placeholder="请输入副单位2(大单位)" ></el-input>
        </el-form-item>
        <el-form-item label="比例2" prop="ratioTwo">
          <el-input v-model="form.ratioTwo" placeholder="请输入比例2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="loadAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>